﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HenrryTools.WinFormTools
{
    public partial class RedisWinForm : Form
    {
        public RedisWinForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// redisIP
        /// </summary>
        public string RedisIP { get; set; } = "127.0.0.1";
        /// <summary>
        /// 数据库
        /// </summary>
        public string DefaultDatabase { get; set; } = "6";
        /// <summary>
        /// 端口
        /// </summary>
        public string Port { get; set; } = "6379";
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RedisWinForm_Load(object sender, EventArgs e)
        {
            txtIP.Text = RedisIP;
            txtDatabase.Text = DefaultDatabase;
            txtPort.Text = Port;
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.RedisWinForm))
            {
                string[] Values= Properties.Settings.Default.RedisWinForm.Split(',');
                txtIP.Text = Values[0];
                txtPort.Text = Values[1];
                txtPassWord.Text = Values[2];
                txtDatabase.Text = Values[3];
                txtKey.Text = Values[4];
                txtDataTime.Text = Values[5];
            }
        }
        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestSend_ClickAsync(object sender, EventArgs e)
        {
            WaitForm1 waitForm1 = new WaitForm1();
            waitForm1.ShowMessage();
            if (!IsRedis()) {
                waitForm1.HideMessage();
                return;
            }
            waitForm1.HideMessage();
        }
        /// <summary>
        /// 判断是否连接成功
        /// </summary>
        /// <param name="Istitle"></param>
        /// <returns></returns>
        public bool IsRedis() {
            bool ReturnState = false;
            var RedisState = RedisConfConAsync().Result;
            if (RedisState)
            {
                labMessage.Text = "连接成功!";
                labMessage.ForeColor = Color.Green;
                ReturnState = true;
                GetSave();
            }
            else
            {
               txtBody.Text = "";
                labMessage.Text = "连接超时,请检查配置是否正确!";
                labMessage.ForeColor = Color.Red;
                ReturnState = false;
            }
            return ReturnState;
        }
        /// <summary>
        /// 请求redis
        /// </summary>
        public async Task<bool> RedisConfConAsync() {
            WaitForm1 waitForm1 = new WaitForm1();
            waitForm1.ShowMessage();
            bool RedisState = false; 
            try
            {
                string redisconf = txtIP.Text + ":" + txtPort.Text + ",password=" + txtPassWord.Text + ",DefaultDatabase=" + txtDatabase.Text;
                Common.RedisHelper.SetCon(redisconf);
                var IsRedis = await Common.RedisHelper.IsRedisAsync();
                if (IsRedis) {
                    RedisState = true;
                }
                else {
                    RedisState = false;
                }
            }
            catch (Exception ex)
            {
                labMessage.Text = "异常:" + ex.Message;
                labMessage.ForeColor = Color.Red;
                RedisState = false;
            }
            waitForm1.HideMessage();
            return RedisState;
        }
        /// <summary>
        /// 调用保存
        /// </summary>
        public void GetSave() {
            Properties.Settings.Default.RedisWinForm = txtIP.Text + "," + txtPort.Text + "," + txtPassWord.Text + "," + txtDatabase.Text + "," + txtKey.Text + "," + txtDataTime.Text;//保存在Settings
            Properties.Settings.Default.Save();//保存
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            WaitForm1 waitForm1 = new WaitForm1();
            waitForm1.ShowMessage();
            if (!IsRedis())
            {
                waitForm1.HideMessage();
                return;
            }
            if (string.IsNullOrWhiteSpace(txtIP.Text) || string.IsNullOrWhiteSpace(txtPort.Text) || string.IsNullOrWhiteSpace(txtDatabase.Text) || string.IsNullOrWhiteSpace(txtKey.Text) || string.IsNullOrWhiteSpace(txtBody.Text))
            {
               txtBody.Text = "";
                labMessage.Text = "RedisIP、端口、数据库 、键Key、内容输入框不能为空!";
                labMessage.ForeColor = Color.Green;
                waitForm1.HideMessage();
                return;
            }
            int RedisDataTime = 0;
            if (!string.IsNullOrWhiteSpace(txtDataTime.Text))
            {
                RedisDataTime = int.Parse(txtDataTime.Text);
            }
            Common.RedisHelper.Set(txtKey.Text, txtBody.Text, RedisDataTime);
            labMessage.Text = "保存成功!";
            labMessage.ForeColor = Color.Green;
            GetSave();
            waitForm1.HideMessage();
        }
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGet_Click(object sender, EventArgs e)
        {
            WaitForm1 waitForm1 = new WaitForm1();
            waitForm1.ShowMessage();
            if (!IsRedis())
            {
                waitForm1.HideMessage();
                return;
            }
            if (string.IsNullOrWhiteSpace(txtIP.Text) || string.IsNullOrWhiteSpace(txtPort.Text) || string.IsNullOrWhiteSpace(txtDatabase.Text) || string.IsNullOrWhiteSpace(txtKey.Text))
            {
               txtBody.Text = "";
                labMessage.Text = "RedisIP、端口、数据库 、键Key输入框不能为空!";
                labMessage.ForeColor = Color.Green;
                waitForm1.HideMessage();
                return;
            }
            if (Common.RedisHelper.Exists(txtKey.Text))
            {
                txtBody.Text = Common.RedisHelper.Get(txtKey.Text).ToString();
                labMessage.Text = "读取成功!";
                labMessage.ForeColor = Color.Green;
                GetSave();
            }
            else
            {
               txtBody.Text = "";
                labMessage.Text = "已过期或不存在  !";
                labMessage.ForeColor = Color.Red;
            }
            waitForm1.HideMessage();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            WaitForm1 waitForm1 = new WaitForm1();
            waitForm1.ShowMessage();
            if (!IsRedis())
            {
                waitForm1.HideMessage();
                return;
            }
            if (string.IsNullOrWhiteSpace(txtIP.Text) || string.IsNullOrWhiteSpace(txtPort.Text) || string.IsNullOrWhiteSpace(txtDatabase.Text) || string.IsNullOrWhiteSpace(txtKey.Text))
            {
               txtBody.Text = "";
                labMessage.Text = "RedisIP、端口、数据库 、键Key输入框不能为空!";
                labMessage.ForeColor = Color.Green;
                waitForm1.HideMessage();
                return;
            }
            if (Common.RedisHelper.Exists(txtKey.Text))
            {
                txtBody.Text = "";;
                Common.RedisHelper.Remove(txtKey.Text).ToString();
                labMessage.Text = "删除成功!";
                labMessage.ForeColor = Color.Green;
                GetSave();
            }
            else
            {
               txtBody.Text = "";
                labMessage.Text = "已过期或不存在  !";
                labMessage.ForeColor = Color.Red;
            }
            waitForm1.HideMessage();
        }
    }
}
